package com.zhuxulin.java.email.mina;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import com.zhuxulin.java.email.mail.SendSimpleEmail;
import com.zhuxulin.java.email.utils.LogUtil;

public class ServerHanlder implements IoHandler {
	// private static final Logger logger = Logger.getLogger(MsgHanlder.class);

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		LogUtil.i("sessionCreated");
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		LogUtil.i("sessionOpened");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		LogUtil.i("sessionClosed");
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		LogUtil.i("sessionIdle");
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		LogUtil.i("exceptionCaught:" + cause.getMessage());
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		LogUtil.i("messageReceived:" + message);

		int cmd = Integer.parseInt(message.toString());
		switch (cmd) {
		case 1:
			SendSimpleEmail.getInstance().sendMail();
			session.write("OK");
			break;

		default:
			break;
		}
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		LogUtil.i("messageSent:" + message);
	}
}